<?php
/**
 * Created by PhpStorm.
 * User: Administrator--Reer
 * Date: 2019/9/10 0010
 * Time: 9:58
 */

namespace app\admin\controller;


use app\common\bean\AdminBean;
use app\common\exception\MyException;
use app\common\manage\LoginManage;
use app\common\utils\CurlUtil;
use app\common\utils\ExitJsonUtil;
use app\common\utils\VcodeUtil;
use Exception;
use think\Controller;
use think\Db;
use think\Response;

class Login extends Controller
{
    /** 登录界面
     * @return mixed
     */
    public function login(){
        //$p = get_num_pages_pdf('upload/lsxtest/t.pdf');
        //p($p);
        return $this->fetch();
    }
    public function testOpenid(){
        $red_url = request()->domain().'/admin/login/testOpenid';
        $user = CurlUtil::getInstance()->getUserInfo('snsapi_base',$red_url);
        if(isset($user['headimgurl'])){
            if( !$u = Db::name('testopenid')->getByopenid($user['openid'])){
                $data['openid'] = $user['openid'];
                $data['name'] = removeEmoji($user['nickname']);
                $data['headerurl'] = $user['headimgurl'];
                Db::name('testopenid')->insert( $data );
                $mess_data = [
                    'first'=>['value'=>'有人关联'],
                    'keyword1'=>['value'=>'有人关联'],
                    'keyword2'=>['value'=>'有人关联'],
                    'keyword3'=>['value'=>''],
                    'keyword4'=>['value'=>'有人关联'],
                ];
                CurlUtil::getInstance()->templateMess('o4ZdX6IGVu00reAxbBiXDHPKknKU', '9Af_uC3pbQJ1MCHe97jRG7ycoswyg0REUkZ_Qv1v0mI', $mess_data);
            }
            die('绑定成功');
        }
        die("请关注《快相博士》公众号");
    }
    public function material(){//素材
        $access_token = CurlUtil::getInstance()->getToken('public');
        $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={$access_token}";
        $data = [
            'type'=>'image',
            'offset'=>0,
            'count'=>20
        ];
        $res = CurlUtil::getInstance()->httpCurl($url, $data);
        p($res);

    }
    public function menu(){
        $access_token = CurlUtil::getInstance()->getToken('public');
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
        $data = [
            "button"=>[
                [
                    'name'=>'在线拍照',
                    'sub_button'=>[
                        [
                            'name'=>'照相',
                            'type'=>'miniprogram',
                            'url'=>'https://www.baidu.com',
                            'appid'=>'wxf8a758957f77f91c',
                            'pagepath'=>'pages/index/index'
                        ],
                        [
                            'name'=>'照相教程',
                            'type'=>'media_id',
                            'media_id'=>'lc9vKNeL5_V4WlmFYS6fGBeVOtuPxekgeLF4Bs2X16I'
                        ]
                    ]
                ],
                [
                    'name'=>'洗相打印',
                    'sub_button'=>[
                        //[
                        //    'name'=>'商城',
                        //    'type'=>'miniprogram',
                        //    'url'=>'https://www.baidu.com',
                        //    'appid'=>'wxa8f5f005149ce337',
                        //    'pagepath'=>'pages/index/index',
                        //],
                        [
                            'name'=>'博士冲印',
                            'type'=>'miniprogram',
                            'url'=>'https://www.baidu.com',
                            'appid'=>'wxe9439fce97ef01ee',
                            'pagepath'=>'pages/index/index/index',
                        ]
                    ]
                ]
            ]
        ];
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $res = CurlUtil::getInstance()->httpCurl($url, $data);
        p($res);
    }
    /**
     * 检测登录
     * @throws Exception
     */
    public function checkLogin(){
        //if(!VcodeUtil::getInstance()->checkCaptcha(request()->post("vcode"))){
        //    MyException::throwMyException("验证码错误");
        //}
        try{
            $adminBean = new AdminBean();
            $adminBean->setData(request()->post());
            $result = LoginManage::getInstance()->adminLogin($adminBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }catch (Exception $exception){
            MyException::throwMyException("账户或密码错误");
        }
    }

    /**
     * 后台用户主动退出，清除保持登录cookie及后台身份session
     */
    public function loginOut(){
        session(null);
        ExitJsonUtil::getInstance()->exitData("登出成功");
    }

    /**获取验证码
     * @return Response
     */
    public function getVcode(){
        return VcodeUtil::getInstance()->getCaptcha();
    }
}